package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserWithdrawRecord;
import com.ruoyi.user.service.IUserWithdrawRecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 提现明细Controller
 * 
 * @author DEREK
 * @date 2023-05-23
 */
@Api("提现管理")
@RestController
@RequestMapping("/user/userWithdraw")
public class UserWithdrawRecordController extends BaseController
{
    @Autowired
    private IUserWithdrawRecordService userWithdrawRecordService;

    /**
     * 查询提现明细列表
     */
    @ApiOperation("查询提现明细列表")
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserWithdrawRecord userWithdrawRecord)
    {
        startPage();
        List<UserWithdrawRecord> list = userWithdrawRecordService.selectUserWithdrawRecordList(userWithdrawRecord);
        return getDataTable(list);
    }

    /**
     * 导出提现明细列表
     */
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:export')")
    @Log(title = "提现明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserWithdrawRecord userWithdrawRecord)
    {
        List<UserWithdrawRecord> list = userWithdrawRecordService.selectUserWithdrawRecordList(userWithdrawRecord);
        ExcelUtil<UserWithdrawRecord> util = new ExcelUtil<UserWithdrawRecord>(UserWithdrawRecord.class);
        util.exportExcel(response, list, "提现明细数据");
    }

    /**
     * 获取提现明细详细信息
     */
    @ApiOperation("获取提现明细详细信息")
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userWithdrawRecordService.selectUserWithdrawRecordById(id));
    }

    /**
     * 新增提现明细
     */
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:add')")
    @Log(title = "提现明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserWithdrawRecord userWithdrawRecord)
    {
        return toAjax(userWithdrawRecordService.insertUserWithdrawRecord(userWithdrawRecord));
    }

    /**
     * 修改提现明细
     */
    @ApiOperation("修改提现明细")
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:edit')")
    @Log(title = "提现明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserWithdrawRecord userWithdrawRecord)
    {
        return toAjax(userWithdrawRecordService.updateUserWithdrawRecord(userWithdrawRecord));
    }

    
    @ApiOperation("提现操作 通过/拒绝")
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:edit')")
    @Log(title = "提现操作 通过/拒绝", businessType = BusinessType.UPDATE)
    @PostMapping("/withdrawalHandle")
    public AjaxResult withdrawalHandle(@RequestBody UserWithdrawRecord userWithdrawRecord)
    {
    	if(null == userWithdrawRecord || null == userWithdrawRecord.getStatus()) {
    		return AjaxResult.error("请选择提现通过与否");
    	}
    	if(null == userWithdrawRecord.getId()) {
    		return AjaxResult.error("请选择提现记录");
    	}
    	if(2 == userWithdrawRecord.getStatus() && StringUtils.isEmpty(userWithdrawRecord.getReason())) {
    		return AjaxResult.error("请输入拒绝理由");
    	}
    	LoginUser loginUser = getLoginUser();
        return userWithdrawRecordService.withdrawalHandle(userWithdrawRecord,loginUser.getUser().getNickName()+"-"+loginUser.getUser().getUserId());
    }

    
    /**
     * 删除提现明细
     */
    @PreAuthorize("@ss.hasPermi('user:userWithdraw:remove')")
    @Log(title = "提现明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userWithdrawRecordService.deleteUserWithdrawRecordByIds(ids));
    }
}
